//////////////////////////////////////////////////////////////////////////

#ifndef _LAYER_H_
#include "layer.h"
#endif

//////////////////////////////////////////////////////////////////////////

#ifndef _CONTROL_H_
#include "control.h"
#endif

#ifndef _INPUT_H_
#include "input.h"
#endif

//#ifndef _SDL_gfxBlitFunc_h
//#include "SDL_gfxBlitFunc.h"
//#endif

//////////////////////////////////////////////////////////////////////////


GUI_Layer::~GUI_Layer()
{

}


//////////////////////////////////////////////////////////////////////////


bool GUI_Layer::Render(SDL_Surface *_mainSurface, bool _repaint)
{
    bool result = false;

    if (_repaint)
    {

        for (unsigned int i = 0; i < m_Labels.size(); ++i)
        {
            m_Labels[i]->Render(_mainSurface);
        }
        for (unsigned int i = 0; i < m_Controls.size(); ++i)
        {
            bool res = m_Controls[i]->Render(_mainSurface);
            result = result || res;
        }

    }

    return result;
}


//////////////////////////////////////////////////////////////////////////


void GUI_Layer::Resize()
{
    for (unsigned int i = 0; i < m_Labels.size(); ++i)
    {
        m_Labels[i]->Resize();
    }
    for (unsigned int i = 0; i < m_Controls.size(); ++i)
    {
        m_Controls[i]->Resize();
    }
}


//////////////////////////////////////////////////////////////////////////


int GUI_Layer::OnKeyChanged(Input* _input)
{
    int result = -2;
    for (unsigned int i = 0; i < m_Controls.size(); ++i)
    {
        switch (m_Controls[i]->OnKeyChanged(_input))
        {
        case GUI_Control::IR_DoAction:
            if (m_FocusedControl != -1)
            {
                m_Controls[m_FocusedControl]->DropFocus();
                m_FocusedControl = -1;
            }
            return i;
            break;
        case GUI_Control::IR_GetFocus:
            if (m_FocusedControl != -1)
                m_Controls[m_FocusedControl]->DropFocus();
            m_FocusedControl = i;
            result = -1;
            break;
        case GUI_Control::IR_NeedRepaint:
            result = -1;
            break;
        default:
            break;
        }
    }
    if (result == -2)
    {
        if (_input->GetKey().m_State == Input::MOUSE_RELEASED)
        {
            if (m_FocusedControl != -1)
            {
                m_Controls[m_FocusedControl]->DropFocus();
                m_FocusedControl = -1;
                result = -1;
            }
        }
    }
    return result;
}


//////////////////////////////////////////////////////////////////////////


void GUI_Layer::SetLabelText(int _control, const std::string& _text)
{
    m_Labels[_control]->SetText(_text);
}


//////////////////////////////////////////////////////////////////////////


const std::string& GUI_Layer::GetLabelText(int _control)
{
    return m_Labels[_control]->GetText();
}


//////////////////////////////////////////////////////////////////////////


void GUI_Layer::SetControlText(int _control, const std::string& _text)
{
    m_Controls[_control]->SetText(_text);
}


//////////////////////////////////////////////////////////////////////////


const std::string& GUI_Layer::GetControlText(int _control)
{
    return m_Controls[_control]->GetText();
}


//////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////


int GUI_Layer::OnMousePositionChanged(Input* _input)
{
    int result = -2;
    for (unsigned int i = 0; i < m_Controls.size(); ++i)
    {
        switch (m_Controls[i]->OnMousePositionChanged(_input))
        {
        case GUI_Control::IR_DoAction:
            return i;
            break;
        case GUI_Control::IR_NeedRepaint:
            result = -1;
            break;
        default:
            break;
        }
    }
    return result;
}


//////////////////////////////////////////////////////////////////////////
